package com.cdut.logistics.model.dao.system;

import java.util.Date;
import java.util.List;

import com.cdut.logistics.model.entity.system.Authority;
/**
 * 
 * @ClassName: authorityDAO  
 * @Description: TODO(权限类的DAO，用于与数据库交互)  
 * @author lxh
 * @date 2018年1月28日 下午2:46:43
 */
public class authorityDAO {
	Authority authority=new Authority();
	/**
	 * 
	 * @Title: addAuthority  
	 * @Description: TODO(增加权限)
	 * @author lxh
	 * @param name
	 * @param url
	 * @param is_use
	 * @param active
	 * @return
	 * @throws
	 */
	public boolean addAuthority(String name,String url,String is_use,String active,Date created){
		boolean result = false;
		try{
			if(authority.findFirst("select * from authority where name=?",name)==null){
		authority.set("name", name);
		authority.set("url", url);
		authority.set("is_use",is_use);
		authority.set("active",active);
		authority.set("created",created);
		result=authority.save();
			}
		}catch(Exception e){};
		return result;
	}
	/**
	 * 
	 * @Title: updateAuthority  
	 * @Description: TODO(更新权限)
	 * @author lxh
	 * @param id
	 * @param name
	 * @param url
	 * @return
	 * @throws
	 */
	public boolean updateAuthority(String id,String name,String url,Date updated){
		try{
			authority=authority.findById(id);
			authority.set("name",name);
			authority.set("url",url);
			authority.set("updated",updated);
		}catch(Exception e){}
		boolean result=authority.update();
		return result;
	}
	/**
	 * 
	 * @Title: queryAuthority  
	 * @Description: TODO(查询权限)
	 * @author lxh
	 * @param id
	 * @return
	 * @throws
	 */
	public Authority queryAuthorityById(String id){
		try{
			authority=authority.findById(id);
		}catch(Exception e){}
		return authority;
	}
	
	/**
	 * 
	 * @Title: deleteAuthority  
	 * @Description: TODO(逻辑删除权限)
	 * @author lxh
	 * @param id
	 * @returnv
	 * @throws
	 */
	public boolean deleteAuthority(String id){
		try{
			authority=authority.findById(id);
			authority.set("active",0);
		}catch(Exception e){}
		boolean result=authority.update();
		return result;
	}
	/**
	 * 
	 * @Title: queryAllAuthority  
	 * @Description: TODO(查询所有权限)
	 * @author lxh
	 * @return
	 * @throws
	 */
	public List<Authority> queryAllAuthority(){
		List<Authority> authorityList=authority.find("select * from authority");
		return authorityList;
	}
	public String queryUrlById(int authority_id) {
		String url=authority.findFirst("SELECT url FROM authority JOIN role ON authority.id=role.Authority_id AND role.is_use=1 AND authority.is_use=1 WHERE role.Authority_id=?",authority_id).getStr("url");
		return url;
	}
}
